﻿using Newtonsoft.Json;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using TradeDataAPI.Core.CacheManager;
using TradeDataAPI.Core.Extensions.AutofacManager;
using TradeDataAPI.Core.Infrastructure;
using TradeDataAPI.Entity.DomainModels;

namespace TradeDataAPI.Core.Common
{
    public class DicKeyValueCache
    {


        public static ICacheService cacheService
        {
            get
            {
                return AutofacContainerModule.GetService<ICacheService>();
            }
        }





        /// <summary>
        /// 获取字典值-缓存2分钟
        /// </summary>
        /// <param name="Name">字典名称</param>
        /// <returns></returns>
        public static List<SysDictionaryList> GetDicKeyValue(string Name, int cacheTime = 60 * 2)
        {
            string Key = $"{Name}";
            var dictionaryLists = new List<SysDictionaryList>();
            if (cacheService.Exists(Key))
            {
                var Json = cacheService.Get(Key);
                dictionaryLists = JsonConvert.DeserializeObject<List<SysDictionaryList>>(Json);
            }
            else
            {
                dictionaryLists = DictionaryManager.GetDictionary(Name).SysDictionaryList.ToList();
                cacheService.Add(Key, JsonConvert.SerializeObject(dictionaryLists), cacheTime);
            }

            return dictionaryLists;
        }
    }
}
